package com.example.homecunter.news.adapter;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

import com.example.homecunter.news.NewsSunFragment;
import com.example.homecunter.news.newsentity.NewsType;

import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * Tablayout+ViewPager（仿今日头条滑动）
 */
public class NewsPagerAdapter extends FragmentPagerAdapter {
    List<NewsType> data1;

    public NewsPagerAdapter(@NonNull @NotNull FragmentManager fm, List<NewsType> data1) {
        super(fm);
        this.data1 = data1;
    }

    /**
     * 展示类型（标题）
     * @param position
     * @return
     */
    @Nullable
    @org.jetbrains.annotations.Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return data1.get(position).getTypename();
    }


    /***
     * 绑定Fragment并传输数据
     * @param position
     * @return
     */
    @NonNull
    @NotNull
    @Override
    public Fragment getItem(int position) {
        NewsSunFragment newsSunFragment = new NewsSunFragment();

        /**
         * 发送所需数据（id）
         */
        Bundle bundle = new Bundle();
        bundle.putInt("type", data1.get(position).getId());
        newsSunFragment.setArguments(bundle);

        return newsSunFragment;
    }

    /**
     * 数据个数
     * @return
     */
    @Override
    public int getCount() {
        return data1.size();
    }
}
